<?php

namespace App\Admin\Metrics\Charts;

use Dcat\Admin\Admin;
use Dcat\Admin\Widgets\ApexCharts\Chart;
use Carbon\Carbon;
use App\Models\Order;

class UserAction extends Chart
{
    public function __construct($containerSelector = null, $options = [])
    {
        parent::__construct($containerSelector, $options);

        $this->setUpOptions();
    }

    /**
     * 初始化图表配置
     */
    protected function setUpOptions()
    {
        $color = Admin::color();

        $colors = ['#FF6666','#008FFB'];
        $title = '24 小时支付行为分析';
        $this->options([
            'colors' => $colors,
            'chart' => [
                'type' => 'line',
                'height' => 430,
                'dropShadow'=>[
                    'enabled'=> true,
                    'color'=>'#000',
                    'top'=> 18,
                    'left'=> 7,
                    'blur'=> 10,
                    'opacity'=> 0.2
                ],
                'toolbar'=>[
                    'show'=>false
                ]
            ],
            'dataLabels'=>[
                'enabled'=>true,
            ],
            'stroke'=>[
                'curve'=>'smooth'
            ],
            'title'=>[
                'text'=>$title,
                'align'=> 'left'
            ],
            'xaxis' => [
                'categories' => [],
            ],
        ]);
    }

    /**
     * 处理图表数据
     */
    protected function buildData()
    {
        // $today = Carbon::today();
        // $start = Carbon::today()->addDays(-30);
        $today = Carbon::now();
        $start = Carbon::today();
        $categories = ['0-1点','1-2点','2-3点','3-4点','4-5点','5-6点','6-7点','7-8点','8-9点','9-10点','10-11点','11-12点','12-13点','13-14点','14-15点','15-16点','16-17点','17-18点','18-19点','19-20点','20-21点','21-22点','22-23点','23-0点'];

        // ->whereDate('created_at', '<', $today)
        // ->whereDate('created_at', '<', $today)
        // ->whereDate('created_at', '<', $today)
        // ->whereDate('created_at', '<', $today)
        // ->whereDate('created_at', '<', $today)
        // ->whereDate('created_at', '<', $today)
        // ->whereDate('created_at', '<', $today)
        // ->whereDate('created_at', '<', $today)
        // ->whereDate('created_at', '<', $today)
        // ->whereDate('created_at', '<', $today)
        // ->whereDate('created_at', '<', $today)
        // ->whereDate('created_at', '<', $today)
        // ->whereDate('created_at', '<', $today)
        // ->whereDate('created_at', '<', $today)
        // ->whereDate('created_at', '<', $today)
        // ->whereDate('created_at', '<', $today)
        // ->whereDate('created_at', '<', $today)
        // ->whereDate('created_at', '<', $today)
        // ->whereDate('created_at', '<', $today)
        // ->whereDate('created_at', '<', $today)
        // ->whereDate('created_at', '<', $today)
        // ->whereDate('created_at', '<', $today)
        // ->whereDate('created_at', '<', $today)
        // ->whereDate('created_at', '<', $today)
       //订单数
       $order_count_01 = Order::whereDate('created_at', '>=', $start)->whereTime('created_at', '>=', '00:00:00')->whereTime('created_at', '<', '00:59:59')->count();
       $order_count_02 = Order::whereDate('created_at', '>=', $start)->whereTime('created_at', '>=', '01:00:00')->whereTime('created_at', '<', '01:59:59')->count();
       $order_count_03 = Order::whereDate('created_at', '>=', $start)->whereTime('created_at', '>=', '02:00:00')->whereTime('created_at', '<', '02:59:59')->count();
       $order_count_04 = Order::whereDate('created_at', '>=', $start)->whereTime('created_at', '>=', '03:00:00')->whereTime('created_at', '<', '03:59:59')->count();
       $order_count_05 = Order::whereDate('created_at', '>=', $start)->whereTime('created_at', '>=', '04:00:00')->whereTime('created_at', '<', '04:59:59')->count();
       $order_count_06 = Order::whereDate('created_at', '>=', $start)->whereTime('created_at', '>=', '05:00:00')->whereTime('created_at', '<', '05:59:59')->count();
       $order_count_07 = Order::whereDate('created_at', '>=', $start)->whereTime('created_at', '>=', '06:00:00')->whereTime('created_at', '<', '06:59:59')->count();
       $order_count_08 = Order::whereDate('created_at', '>=', $start)->whereTime('created_at', '>=', '07:00:00')->whereTime('created_at', '<', '07:59:59')->count();
       $order_count_09 = Order::whereDate('created_at', '>=', $start)->whereTime('created_at', '>=', '08:00:00')->whereTime('created_at', '<', '08:59:59')->count();
       $order_count_10 = Order::whereDate('created_at', '>=', $start)->whereTime('created_at', '>=', '09:00:00')->whereTime('created_at', '<', '09:59:59')->count();
       $order_count_11 = Order::whereDate('created_at', '>=', $start)->whereTime('created_at', '>=', '10:00:00')->whereTime('created_at', '<', '10:59:59')->count();
       $order_count_12 = Order::whereDate('created_at', '>=', $start)->whereTime('created_at', '>=', '11:00:00')->whereTime('created_at', '<', '11:59:59')->count();
       $order_count_13 = Order::whereDate('created_at', '>=', $start)->whereTime('created_at', '>=', '12:00:00')->whereTime('created_at', '<', '12:59:59')->count();
       $order_count_14 = Order::whereDate('created_at', '>=', $start)->whereTime('created_at', '>=', '13:00:00')->whereTime('created_at', '<', '13:59:59')->count();
       $order_count_15 = Order::whereDate('created_at', '>=', $start)->whereTime('created_at', '>=', '14:00:00')->whereTime('created_at', '<', '14:59:59')->count();
       $order_count_16 = Order::whereDate('created_at', '>=', $start)->whereTime('created_at', '>=', '15:00:00')->whereTime('created_at', '<', '15:59:59')->count();
       $order_count_17 = Order::whereDate('created_at', '>=', $start)->whereTime('created_at', '>=', '16:00:00')->whereTime('created_at', '<', '16:59:59')->count();
       $order_count_18 = Order::whereDate('created_at', '>=', $start)->whereTime('created_at', '>=', '17:00:00')->whereTime('created_at', '<', '17:59:59')->count();
       $order_count_19 = Order::whereDate('created_at', '>=', $start)->whereTime('created_at', '>=', '18:00:00')->whereTime('created_at', '<', '18:59:59')->count();
       $order_count_20 = Order::whereDate('created_at', '>=', $start)->whereTime('created_at', '>=', '19:00:00')->whereTime('created_at', '<', '19:59:59')->count();
       $order_count_21 = Order::whereDate('created_at', '>=', $start)->whereTime('created_at', '>=', '20:00:00')->whereTime('created_at', '<', '20:59:59')->count();
       $order_count_22 = Order::whereDate('created_at', '>=', $start)->whereTime('created_at', '>=', '21:00:00')->whereTime('created_at', '<', '21:59:59')->count();
       $order_count_23 = Order::whereDate('created_at', '>=', $start)->whereTime('created_at', '>=', '22:00:00')->whereTime('created_at', '<', '22:59:59')->count();
       $order_count_24 = Order::whereDate('created_at', '>=', $start)->whereTime('created_at', '>=', '23:00:00')->whereTime('created_at', '<', '23:59:59')->count();
       
       //成功订单数
       $order_paid_count_01 = Order::paid()->whereDate('paid_at', '>=', $start)->whereTime('paid_at', '>=', '00:00:00')->whereTime('paid_at', '<', '00:59:59')->count();
       $order_paid_count_02 = Order::paid()->whereDate('paid_at', '>=', $start)->whereTime('paid_at', '>=', '01:00:00')->whereTime('paid_at', '<', '01:59:59')->count();
       $order_paid_count_03 = Order::paid()->whereDate('paid_at', '>=', $start)->whereTime('paid_at', '>=', '02:00:00')->whereTime('paid_at', '<', '02:59:59')->count();
       $order_paid_count_04 = Order::paid()->whereDate('paid_at', '>=', $start)->whereTime('paid_at', '>=', '03:00:00')->whereTime('paid_at', '<', '03:59:59')->count();
       $order_paid_count_05 = Order::paid()->whereDate('paid_at', '>=', $start)->whereTime('paid_at', '>=', '04:00:00')->whereTime('paid_at', '<', '04:59:59')->count();
       $order_paid_count_06 = Order::paid()->whereDate('paid_at', '>=', $start)->whereTime('paid_at', '>=', '05:00:00')->whereTime('paid_at', '<', '05:59:59')->count();
       $order_paid_count_07 = Order::paid()->whereDate('paid_at', '>=', $start)->whereTime('paid_at', '>=', '06:00:00')->whereTime('paid_at', '<', '06:59:59')->count();
       $order_paid_count_08 = Order::paid()->whereDate('paid_at', '>=', $start)->whereTime('paid_at', '>=', '07:00:00')->whereTime('paid_at', '<', '07:59:59')->count();
       $order_paid_count_09 = Order::paid()->whereDate('paid_at', '>=', $start)->whereTime('paid_at', '>=', '08:00:00')->whereTime('paid_at', '<', '08:59:59')->count();
       $order_paid_count_10 = Order::paid()->whereDate('paid_at', '>=', $start)->whereTime('paid_at', '>=', '09:00:00')->whereTime('paid_at', '<', '09:59:59')->count();
       $order_paid_count_11 = Order::paid()->whereDate('paid_at', '>=', $start)->whereTime('paid_at', '>=', '10:00:00')->whereTime('paid_at', '<', '10:59:59')->count();
       $order_paid_count_12 = Order::paid()->whereDate('paid_at', '>=', $start)->whereTime('paid_at', '>=', '11:00:00')->whereTime('paid_at', '<', '11:59:59')->count();
       $order_paid_count_13 = Order::paid()->whereDate('paid_at', '>=', $start)->whereTime('paid_at', '>=', '12:00:00')->whereTime('paid_at', '<', '12:59:59')->count();
       $order_paid_count_14 = Order::paid()->whereDate('paid_at', '>=', $start)->whereTime('paid_at', '>=', '13:00:00')->whereTime('paid_at', '<', '13:59:59')->count();
       $order_paid_count_15 = Order::paid()->whereDate('paid_at', '>=', $start)->whereTime('paid_at', '>=', '14:00:00')->whereTime('paid_at', '<', '14:59:59')->count();
       $order_paid_count_16 = Order::paid()->whereDate('paid_at', '>=', $start)->whereTime('paid_at', '>=', '15:00:00')->whereTime('paid_at', '<', '15:59:59')->count();
       $order_paid_count_17 = Order::paid()->whereDate('paid_at', '>=', $start)->whereTime('paid_at', '>=', '16:00:00')->whereTime('paid_at', '<', '16:59:59')->count();
       $order_paid_count_18 = Order::paid()->whereDate('paid_at', '>=', $start)->whereTime('paid_at', '>=', '17:00:00')->whereTime('paid_at', '<', '17:59:59')->count();
       $order_paid_count_19 = Order::paid()->whereDate('paid_at', '>=', $start)->whereTime('paid_at', '>=', '18:00:00')->whereTime('paid_at', '<', '18:59:59')->count();
       $order_paid_count_20 = Order::paid()->whereDate('paid_at', '>=', $start)->whereTime('paid_at', '>=', '19:00:00')->whereTime('paid_at', '<', '19:59:59')->count();
       $order_paid_count_21 = Order::paid()->whereDate('paid_at', '>=', $start)->whereTime('paid_at', '>=', '20:00:00')->whereTime('paid_at', '<', '20:59:59')->count();
       $order_paid_count_22 = Order::paid()->whereDate('paid_at', '>=', $start)->whereTime('paid_at', '>=', '21:00:00')->whereTime('paid_at', '<', '21:59:59')->count();
       $order_paid_count_23 = Order::paid()->whereDate('paid_at', '>=', $start)->whereTime('paid_at', '>=', '22:00:00')->whereTime('paid_at', '<', '22:59:59')->count();
       $order_paid_count_24 = Order::paid()->whereDate('paid_at', '>=', $start)->whereTime('paid_at', '>=', '23:00:00')->whereTime('paid_at', '<', '23:59:59')->count();
        // 执行你的数据查询逻辑
        $data = [
            [
                'name'=> "提交订单数",
                'data' => [
                    $order_count_01,
                    $order_count_02,
                    $order_count_03,
                    $order_count_04,
                    $order_count_05,
                    $order_count_06,
                    $order_count_07,
                    $order_count_08,
                    $order_count_09,
                    $order_count_10,
                    $order_count_11,
                    $order_count_12,
                    $order_count_13,
                    $order_count_14,
                    $order_count_15,
                    $order_count_16,
                    $order_count_17,
                    $order_count_18,
                    $order_count_19,
                    $order_count_20,
                    $order_count_21,
                    $order_count_22,
                    $order_count_23,
                    $order_count_24,
                ]
            ],
            [
                'name'=> "支付订单数",
                'data' => [
                    $order_paid_count_01,
                    $order_paid_count_02,
                    $order_paid_count_03,
                    $order_paid_count_04,
                    $order_paid_count_05,
                    $order_paid_count_06,
                    $order_paid_count_07,
                    $order_paid_count_08,
                    $order_paid_count_09,
                    $order_paid_count_10,
                    $order_paid_count_11,
                    $order_paid_count_12,
                    $order_paid_count_13,
                    $order_paid_count_14,
                    $order_paid_count_15,
                    $order_paid_count_16,
                    $order_paid_count_17,
                    $order_paid_count_18,
                    $order_paid_count_19,
                    $order_paid_count_20,
                    $order_paid_count_21,
                    $order_paid_count_22,
                    $order_paid_count_23,
                    $order_paid_count_24,
                ]
            ]
        ];
        $this->withData($data);
        $this->withCategories($categories);
    }

    /**
     * 设置图表数据
     *
     * @param array $data
     *
     * @return $this
     */
    public function withData(array $data)
    {
        return $this->option('series', $data);
    }

    /**
     * 设置图表类别.
     *
     * @param array $data
     *
     * @return $this
     */
    public function withCategories(array $data)
    {
        return $this->option('xaxis.categories', $data);
    }

    /**
     * 渲染图表
     *
     * @return string
     */
    public function render()
    {
        $this->buildData();

        return parent::render();
    }
}